﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataEntity;
using Model;
using System.Collections.ObjectModel;

namespace SWA_View
{
    public partial class ProjectList : Form
    {
        public ProjectList()
        {
            InitializeComponent();
        }
        ObservableCollection<ProjectEntity> DataSource = null;
        private void ProjectList_Load(object sender, EventArgs e)
        {
            dtp_Begin.Text = (new DateTime(DateTime.Now.Year - 1, DateTime.Now.Month, DateTime.Now.Day)).ToShortDateString();
            Login login = new Login();
            DialogResult rslt = login.ShowDialog(this);
            if (rslt == System.Windows.Forms.DialogResult.OK)
            {
                btnRefresh_Click(null, null);
            }
            else
            {
                this.Close();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (Panel_Search.Tag.Equals("E"))
            {
                Panel_Search.Tag = "C";                
            }
            else if (Panel_Search.Tag.Equals("C"))//由折叠状态切换为展开状态
            {
                Panel_Search.Tag = "E";
            }
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (Panel_Search.Tag.ToString())
            {
                case "E": //由折叠状态切换为展开状态
                    //切换图片
                    pictureBox1.Image = new Bitmap(Properties.Resources.Expand);
                    Panel_Search.Width += 5;
                    Point p = pictureBox1.Location;
                    p.X += 5;
                    pictureBox1.Location = p;
                    Point pl = lv_ProjectList.Location;
                      pl.X  += 5;
                      lv_ProjectList.Location = pl;
                      lv_ProjectList.Width -= 5;
                    if (Panel_Search.Width >= 200)
                    {
                        timer1.Enabled = false;
                    }
                    break;
                case "C": //由展开状态切换为折叠状态
                    pictureBox1.Image = new Bitmap(Properties.Resources.收缩);                     
                    Panel_Search.Width -= 5;
                    Point p2 = pictureBox1.Location;
                    p2.X -= 5;
                    pictureBox1.Location = p2;
                    lv_ProjectList.Width += 5;
                     Point pl2 = lv_ProjectList.Location;
                      pl2.X  -= 5;
                      //lv_ProjectList.Width += 5;
                      lv_ProjectList.Location = pl2;
                    if (Panel_Search.Width <= 30)
                    {
                        timer1.Enabled = false;
                    }
                    break;
            }
        }

        private void btnNewProject_Click(object sender, EventArgs e)
        {
            ProjectInfo pi = new ProjectInfo(0, DictionaryEnum.New);
            pi.FormClosed += new FormClosedEventHandler(pi_FormClosed);
            DialogResult dr = pi.ShowDialog(this);
        }

        void pi_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.btnRefresh_Click(null, null);
        }
        MainForm _mf = null; 
        private void btn_Start_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection collection = lv_ProjectList.SelectedItems;
            if (collection.Count == 1)
            {
                int id = Convert.ToInt32((collection[0] as ListViewItem).Tag.ToString());

                _mf = new MainForm(id);
                _mf.FormClosed += new FormClosedEventHandler(_mf_FormClosed);
                this.Visible = false;
                _mf.ShowDialog(this);
            }
            else
            {
                MessageBox.Show("请选择实验项目!");
            }
          
           
        }

        void _mf_FormClosed(object sender, FormClosedEventArgs e)
        {
            EDSDKLib.EDSDK.EdsTerminateSDK();//关闭相机
            

            MainForm mf = sender as MainForm;
            DS40xxSDK.HikVisionSDK.ChannelClose(mf.Ch1);
            DS40xxSDK.HikVisionSDK.ChannelClose(mf.Ch2);
            DS40xxSDK.HikVisionSDK.ChannelClose(mf.Ch3);
            DS40xxSDK.HikVisionSDK.ChannelClose(mf.Ch4);
            DS40xxSDK.HikVisionSDK.ChannelClose(mf.Ch5);
            if (mf != null && mf.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                this.Visible = true;
            }
            else
            {
                ///this.Close();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.lv_ProjectList.Items.Clear();
            CommonModel pe = new CommonModel();
            DataSource = pe.Query<ProjectEntity>("Status = 1 ");
            //lv_ProjectList.Items = DataSource;
            ListViewItem lvi = null;

            for (int x = 0; x < DataSource.Count; x++)
            {
                lvi = new ListViewItem((x + 1) + "");
                lvi.Tag = DataSource[x].ID;
                //lvi.SubItems.Add((x + 1) + "");
                lvi.SubItems.Add(DataSource[x].ProjectNo);
                lvi.SubItems.Add(DataSource[x].ProjectHost);
                lvi.SubItems.Add(DataSource[x].ModelNo);
                lvi.SubItems.Add(DataSource[x].Client);
                lvi.SubItems.Add(DataSource[x].Rate);
                lvi.SubItems.Add(DataSource[x].ShipType);
                lvi.SubItems.Add(DataSource[x].CreateDate.ToShortDateString());
                this.lv_ProjectList.Items.Add(lvi);
            }
            pe = null;
        }

        private void btnEditProject_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection collection = lv_ProjectList.SelectedItems;
            if (collection.Count == 1)
            {
                int id = Convert.ToInt32((collection[0] as ListViewItem).Tag.ToString());
                ProjectInfo pi = new ProjectInfo(id, DictionaryEnum.Edit);
                pi.FormClosed += new FormClosedEventHandler(pi_FormClosed);
                DialogResult dr = pi.ShowDialog(this);
            }
            else
            {
                MessageBox.Show("请选择编辑项目!");
            }
        }

        private void btnViewProject_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection collection = lv_ProjectList.SelectedItems;
            if (collection.Count == 1)
            {
                int id = Convert.ToInt32((collection[0] as ListViewItem).Tag.ToString());
                ProjectInfo pi = new ProjectInfo(id, DictionaryEnum.View);
                pi.FormClosed += new FormClosedEventHandler(pi_FormClosed);
                DialogResult dr = pi.ShowDialog(this);
            }
            else
            {
                MessageBox.Show("请选择编辑项目!");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
             ListView.SelectedListViewItemCollection collection = lv_ProjectList.SelectedItems;
             if (collection.Count == 1)
             {
                 DialogResult dr = MessageBox.Show("确定删除该项目?", "", MessageBoxButtons.OKCancel);
                 if (dr == System.Windows.Forms.DialogResult.OK)
                 {
                     int id = Convert.ToInt32((collection[0] as ListViewItem).Tag.ToString());                     
                     ProjectModel pm = new ProjectModel();
                     pm.Detele(id);
                     MessageBox.Show("删除成功！");
                     this.btnRefresh_Click(null, null);
                 }
                 else
                 {
                     MessageBox.Show("请选择编辑项目!");
                 }
             }
        }

       

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string _ProjectNo = tb_ProjectNo.Text;
            string Host = tb_ProjectHost.Text;
            string ModeNo = tb_ModelNo.Text;
            string client = tb_client.Text;
            string shiptype = comboShipType.SelectedItem == null ? "" : comboShipType.SelectedItem.ToString();
            DateTime stDt = DateTime.Parse(dtp_Begin.Text);
            DateTime endDt = DateTime.Parse(dtp_End.Text);
            ProjectModel pm = new ProjectModel();
            string SearchCodition = string.Format(@"ProjectNo like '%{0}%' and ProjectHost like '%{1}%'  
and ModelNo like '%{2}%' 
  and Client like '%{3}%'
  and ShipType like '%{4}%'
  and CreateDate  >= '{5}'
  and CreateDate <= '{6}'
and Status=1", _ProjectNo, Host, ModeNo, client, shiptype, stDt.ToShortDateString(), endDt.ToShortDateString());
            DataSource = pm.GetProjectEntity(SearchCodition);
            this.lv_ProjectList.Items.Clear();
            ProjectModel pe = new ProjectModel();
            //DataSource = pe.GetProjectEntity("Status = 1 ");
            //lv_ProjectList.Items = DataSource;
            ListViewItem lvi = null;

            for (int x = 0; x < DataSource.Count; x++)
            {
                lvi = new ListViewItem((x + 1) + "");
                lvi.Tag = DataSource[x].ID;
                //lvi.SubItems.Add((x + 1) + "");
                lvi.SubItems.Add(DataSource[x].ProjectNo);
                lvi.SubItems.Add(DataSource[x].ProjectHost);
                lvi.SubItems.Add(DataSource[x].ModelNo);
                lvi.SubItems.Add(DataSource[x].Client);
                lvi.SubItems.Add(DataSource[x].Rate);
                lvi.SubItems.Add(DataSource[x].ShipType);
                lvi.SubItems.Add(DataSource[x].CreateDate.ToShortDateString());
                this.lv_ProjectList.Items.Add(lvi);
            }
            pe = null;
        }

        
    }
}
